﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.OpenAPI.Opml;

namespace DNA.Mvc.Services
{
    public class OpmlDocumentGenerator : CommandContainer
    {
        protected OpmlDocument opml;

        public OpmlDocumentGenerator(IEnumerable<OpmlOutlineGeneratorBase> outlines) : this() { this.InnerItems.AddRange(outlines); }

        public OpmlDocumentGenerator() { Reset(); }

        public OpmlDocumentGenerator Reset()
        {
            opml = new OpmlDocument()
            {
                Head = new OpmlHead()
                {
                    Created = DateTime.UtcNow,
                    Modified = DateTime.UtcNow,
                    VertScrollState = 1,
                    WindowRight = 800,
                    WindowBottom = 600
                },
                Body = new OpmlBody()
                {
                    Outlines = new List<OpmlOutline>()
                }
            };
            return this;
        }

        public OpmlDocumentGenerator SetOwner(string name, string id = "", string email = "")
        {
            opml.Head.OwnerID = id;
            opml.Head.OwnerEmail = email;
            opml.Head.OwnerName = name;
            return this;
        }

        public OpmlDocumentGenerator SetWindowSize(int top = 0, int left = 0, int right = 800, int bottom = 600)
        {
            if (bottom > 0)
                opml.Head.WindowBottom = bottom;
            opml.Head.WindowLeft = left;
            opml.Head.WindowRight = right;
            if (top > 0)
                opml.Head.WindowTop = top;
            return this;
        }

        public OpmlDocumentGenerator SetTitle(string title)
        {
            opml.Head.Title = title;
            return this;
        }

        public OpmlDocumentGenerator SetCreated(DateTime date)
        {
            this.opml.Head.Created = date;
            return this;
        }

        public OpmlDocumentGenerator SetModified(DateTime date)
        {
            this.opml.Head.Modified = date;
            return this;
        }

        public OpmlDocument GetOpmlDoc()
        {
            Invoke(new CommandContext() { Bag = opml });
            return opml;
        }

        public string GetXml()
        {
            return DNA.Utility.XmlSerializerUtility.SerializeToXml<OpmlDocument>(GetOpmlDoc());
        }
    }

}
